package com.train.system.facade;

import cn.hutool.core.bean.BeanUtil;
import com.train.common.group.AddGroup;
import com.train.common.group.EditGroup;
import com.train.common.group.QueryGroup;
import com.train.common.resp.CommonResp;
import com.train.common.resp.PageResp;
import com.train.system.dto.seat.TrainGenerateSeatDTO;
import com.train.system.dto.train.TrainAddOrEditDTO;
import com.train.system.dto.train.TrainCodeDTO;
import com.train.system.dto.train.TrainPageDTO;
import com.train.system.req.train.TrainAddOrEditReq;
import com.train.system.req.train.TrainCodeReq;
import com.train.system.req.train.TrainGenerateSeatReq;
import com.train.system.req.train.TrainPageReq;
import com.train.system.resp.train.TrainCodeResp;
import com.train.system.resp.train.TrainDetailResp;
import com.train.system.resp.train.TrainPageResp;
import com.train.system.service.TrainService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * train Facade 实现类
 * @author baitiaojun
 * @createDate 2025-02-20 01:04:22
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("train")
public class TrainFacade {

    private final TrainService trainService;

    @PostMapping("page")
    public CommonResp pageList(@RequestBody @Validated(QueryGroup.class) TrainPageReq req) {
        TrainPageDTO trainPageDTO = BeanUtil.copyProperties(req, TrainPageDTO.class);
        PageResp<TrainPageResp> pageResp = trainService.pageList(trainPageDTO);
        return CommonResp.ok(pageResp);
    }

    @PostMapping("add")
    public CommonResp add(@RequestBody @Validated(AddGroup.class) TrainAddOrEditReq req) {
        TrainAddOrEditDTO trainAddOrEditDTO = BeanUtil.copyProperties(req, TrainAddOrEditDTO.class);
        trainService.addOrEditTrain(trainAddOrEditDTO);
        return CommonResp.ok();
    }

    @PostMapping("edit")
    public CommonResp edit(@RequestBody @Validated(EditGroup.class) TrainAddOrEditReq req) {
        TrainAddOrEditDTO trainAddOrEditDTO = BeanUtil.copyProperties(req, TrainAddOrEditDTO.class);
        trainService.addOrEditTrain(trainAddOrEditDTO);
        return CommonResp.ok();
    }

    @DeleteMapping("remove/{id}")
    public CommonResp delete(@PathVariable("id") Long id) {
        trainService.remove(id);
        return CommonResp.ok();
    }

    @PostMapping("page-code")
    public CommonResp pageCodeList(@RequestBody @Validated(QueryGroup.class) TrainCodeReq req) {
        TrainCodeDTO trainCodeDTO = BeanUtil.copyProperties(req, TrainCodeDTO.class);
        PageResp<TrainCodeResp> pageResp = trainService.pageCodeList(trainCodeDTO);
        return CommonResp.ok(pageResp);
    }

    @PostMapping("generate-seat")
    public CommonResp generateTrainSeat(@RequestBody @Validated(AddGroup.class) TrainGenerateSeatReq req) {
        TrainGenerateSeatDTO trainGenerateSeatDTO = BeanUtil.copyProperties(req, TrainGenerateSeatDTO.class);
        trainService.generateTrainSeat(trainGenerateSeatDTO);
        return CommonResp.ok();
    }

    @PostMapping("detail/{trainCode}")
    public CommonResp getTrainDetail(@PathVariable("trainCode") String trainCode) {
        TrainDetailResp trainDetailResp = trainService.getTrainDetail(trainCode);
        return CommonResp.ok(trainDetailResp);
    }
}
